
const lodash = require("lodash")
const loginFns = require("../common/login")
import { pageFns } from '../utils/pageHelper'
import { SourceDao } from '@/dao/sourceDao'
import { ArticleStatus } from '@/entities/Constant'

const SELECTORS = { "ITEMLINK": ".list-view .product-item .product-image", "NEXTPAGE": ".next-pagination-pages .next" }

const LOGINS = {}

async function goSpider(page, task, source, rule) {
    let selectors = JSON.parse(rule.codeRule)
    if (lodash.isEmpty(selectors)) {
        return { err: "there is not selectors of row:" + JSON.stringify(source) }
    }
    try {
        //是否需要登录
        if (task.accountId) {
            let res = await loginFns.checkLogin(page, task, LOGINS)
            if (res.err) {
                console.log("goSpider.checkLogin", res)
                return res
            }
        }
        let res = await pageFns.runResetTimout(source.link, page, async (pageUrl, page) => {
            await page.goto(pageUrl, { waitUntil: 'networkidle2' })
            return { err: null }
        }, { timeout: 60000 })
        if (res.err) {
            throw new Error(res.err)
        }
        let hrefDict = {}
        await fetchItems(source, page, hrefDict, 1, source.maxRow, selectors)
        return { data: hrefDict }
    } catch (err) {
        return { err: err }
    }
}

async function fetchItems(row, page, hrefDict, pageNum, rowMax, selectors) {
    // 获取item的href
    let hrefs = await pageFns.attributeNames(page, selectors.ITEMLINK, "href")
    for (let href of hrefs) {
        if (!hrefDict[href]) {
            // 存入source中
            let obj = SourceDao.toDetail(row, href)
            let res = await SourceDao.save(obj)
            if (!res.err) {
                hrefDict[href] = 1
            }
        }
    }
    //点击上一页
    if (rowMax >= pageNum) {
        await pageFns.waitAndClick(page, selectors.NEXTPAGE)
        pageNum = pageNum + 1
        await fetchItems(row, page, hrefDict, pageNum, rowMax, selectors)
    } else {
        row.status = ArticleStatus.Gather
        row.count = Object.keys(hrefDict).length;
        SourceDao.save(row)
    }
}

export { goSpider }